package com.yjtc.cbg.imageloader;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yjtc.cbg.imageloader.bean.FolderBean;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private List<String> mImages;

    private List<FolderBean> mBeans;

    private RecyclerView mContentView;
    private RelativeLayout mButtomLayout;
    private TextView mSumImage;
    private TextView mSumTextImage;
    private ProgressDialog mDialog;

    private Handler mUIHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==0x110){
                mDialog.dismiss();
                dataToView();
            }
        }
    };

    private File currentFile;
    private int maxCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {

    }

    private void dataToView() {
        if(currentFile==null){
            Toast.makeText(MainActivity.this,"未扫描到任何图片",Toast.LENGTH_SHORT).show();
            return;
        }
        mImages = Arrays.asList(currentFile.list());
    }

    private void initData() {
        mDialog = new ProgressDialog(MainActivity.this);
        mDialog.setMessage("正在加载......");
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            Toast.makeText(MainActivity.this,"当前存储卡不可用",Toast.LENGTH_LONG).show();
        }
        mDialog.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Uri mUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver resolver = getContentResolver();
                Cursor query = resolver.query(mUri, null, MediaStore.Images.Media.MIME_TYPE + "=? or "
                                + MediaStore.Images.Media.MIME_TYPE + "=?", new String[]{"image/jpeg", "image/png"},
                        MediaStore.Images.Media.DATE_MODIFIED);

                Set<String> images = new HashSet<String>();
                while (query.moveToNext()){
                    String path = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA));
                    File file = new File(path).getParentFile();
                    if(file==null)
                        continue;
                    FolderBean bean = null;
                    String dirPath = file.getAbsolutePath();
                    if(images.contains(dirPath)){
                        continue;
                    }else {
                        images.add(dirPath);
                        bean = new FolderBean();
                        bean.setmDir(dirPath);
                        bean.setmFirstImagePath(path);

                    }
                    if(file.list()==null)
                        continue;
                    int fileSize=file.list(new FilenameFilter() {
                        @Override
                        public boolean accept(File file, String s) {
                            if(s.endsWith(".jpg")||s.endsWith(".jpeg")||s.endsWith(".png"))
                                return true;
                            return false;
                        }
                    }).length;
                    bean.setmImageCount(fileSize);
                    mBeans.add(bean);

                    if(fileSize>maxCount){
                        maxCount = fileSize;
                        currentFile = file;
                    }
                }

                query.close();
                images = null;
                mUIHandler.sendEmptyMessage(0x110);
            }
        }).start();
    }

    private void initView() {
        mContentView = (RecyclerView) findViewById(R.id.id_address_view);
        mButtomLayout = (RelativeLayout) findViewById(R.id.id_buttom);
        mSumImage = (TextView) findViewById(R.id.id_sum_image);
        mSumTextImage = (TextView) findViewById(R.id.id_sum_image_text);
    }

}
